<template>
  <el-dialog title="添加员工" :visible.sync="isShow" width="30%">
    <el-form ref="from" label-width="120px" :model="from" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="from.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="from.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="from.timeOfEntry" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="from.formOfEmployment">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="from.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <div v-outClick="outTree">
          <el-input
            v-model="from.departmentName"
            readonly
            @focus="Chick_focus"
          />
          <!-- 树形控件 -->
          <el-tree
            v-if="treeShow"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="from.correctionTime" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <div style="taxt-align: center">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </span>
  </el-dialog>
</template>

<script>
import employees from '@/api/constant/employees'
import { companyDepartmentAPI, sysuserPOSTAPI } from '@/api'

export default {
  filters: {
    getTime() {
      return
    }
  },
  data() {
    return {
      isShow: false,
      employees,
      from: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      treeData: [],
      treeShow: false,
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    async Chick_focus() {
      if (this.treeData.length === 0) {
        const res = await companyDepartmentAPI()
        this.treeData = this.changeData(res.data.depts, '')
      }

      this.treeShow = true
      console.log(this.treeData)
    },
    changeData(arr, pid = '') {
      return arr.filter((v) => {
        if (v.pid == pid) {
          v.children = this.changeData(arr, v.id)
          return true
        }
      })
    },
    nodeClick(data) {
      this.treeShow = false
      this.from.departmentName = data.name
    },
    outTree() {
      this.treeShow = false
    },
    // 表单验证 提交
    submit() {
      this.$refs.from.validate(async(result) => {
        if (result) {
         await sysuserPOSTAPI(this.from)
          this.$emit('getData')
          this.$message.success('添加成功')
          this.isShow = false
        }
      })
    }
  }
}
</script>

<style lang="scss"></style>
